package com.gec.shop;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ServerSocket serverSocket = new ServerSocket(9996);
                    while(true){
                        Socket socket = serverSocket.accept();
                        Thread thread = new Thread(new SocketThread(socket));
                        thread.start();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        Scanner scanner = new Scanner(System.in);
        while(true){
            String str = scanner.next();
            if(str.equals("退出")){
                scanner.close();
                return;
            }
        }
    }
}
class SocketThread implements Runnable{
    Socket socket;
    public SocketThread(){}
    public SocketThread(Socket socket){
        this.socket = socket;
    }
    //用于跟客户端交互
    @Override
    public void run() {
        System.out.println("---"+socket.getInetAddress().getHostAddress()+"会话已经连接----");
        try {
            PrintWriter out = new PrintWriter(socket.getOutputStream(),true);//打印流
            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());//序列号流
            while(true){
                TransferFiles transferFiles = (TransferFiles)in.readObject();//接受客户端对象
                System.out.println("传递过来的是"+transferFiles.getTarget());
                if(transferFiles.getTarget().contains("register")){//进入注册通道
                    System.out.println("---"+socket.getInetAddress().getHostAddress()+"进入注册通道");
                    out.println(Sql.addUser(transferFiles.getDatas().get(transferFiles.getTarget())));//向数据库添加数据进行注册并且回调操作状态
                }else if(transferFiles.getTarget().contains("login")){
                    System.out.println("---"+socket.getInetAddress().getHostAddress()+"进入登录通道");
                    out.println(Sql.userLogin(transferFiles.getDatas().get(transferFiles.getTarget())));//进行数据库查询并返回信息
                }else if(transferFiles.getTarget().contains("query_commodity")){
                    System.out.println("id为："+transferFiles.getId()+"的会员正在进行商品操作");
                    out.println(Sql.queryCommodity());
                }else if(transferFiles.getTarget().contains("member_shoppingCart_add")){
                    System.out.println("id为："+transferFiles.getId() + "的会员正在进行添加购物车的操作");
                    out.println(Sql.addShoppingCart(transferFiles.getId(),(Commodity) transferFiles.getDatas().get(transferFiles.getTarget())));
                }else if(transferFiles.getTarget().contains("member_shoppingCart_query")){
                    System.out.println("id为："+transferFiles.getId() + "的会员正在进行查询购物车的操作");
                    out.println(Sql.queryShoppingCart(transferFiles.getId()));
                }else if(transferFiles.getTarget().contains("member_shoppingCart_xg")){
                    System.out.println("id为："+transferFiles.getId() + "的会员正在进行修改购物车的操作");
                    out.println(Sql.XgShoppingCart(transferFiles.getId(),(Commodity)transferFiles.getDatas().get(transferFiles.getTarget())));
                } else if(transferFiles.getTarget().contains("member_shoppingCart_delect")){
                    System.out.println("id为："+transferFiles.getId() + "的会员正在进行删除购物车的操作");
                    out.println(Sql.delectShoppingCart(transferFiles.getId(),(Commodity)transferFiles.getDatas().get(transferFiles.getTarget())));
                }else if(transferFiles.getTarget().contains("member_order_make")){
                    System.out.println("id为："+transferFiles.getId()+"的会员正在进行创建订单的操作");
                    out.println(Sql.markOrder(transferFiles.getId()));
                }else if(transferFiles.getTarget().contains("member_addBalance")){
                    System.out.println("id为："+transferFiles.getId()+"的会员正在进行充值的操作");
                    out.println(Sql.addBalance(transferFiles.getId(),transferFiles.getDatas().get(transferFiles.getTarget())));
                }else if((transferFiles.getTarget().contains("staff_add_commodity"))){
                    System.out.println("id为："+transferFiles.getId()+"的员工正在进行商品入库的操作");
                    out.println(Sql.addCommodity((Commodity)transferFiles.getDatas().get(transferFiles.getTarget())));
                }else if((transferFiles.getTarget().contains("staff_modity_commodity"))){
                    System.out.println("id为："+transferFiles.getId()+"的员工正在进行修改商品的操作");
                    out.println(Sql.modifyCommodity((Commodity) transferFiles.getDatas().get(transferFiles.getTarget())));
                }else if((transferFiles.getTarget().contains("staff_clockIn"))){
                    System.out.println("id为："+transferFiles.getId()+"的员工正在进行打卡的操作");
                    out.println(Sql.clockIn(transferFiles.getId()));
                }else if((transferFiles.getTarget().contains("staff_attendanceRecord"))){
                    System.out.println("id为："+transferFiles.getId()+"的员工正在进行考勤的操作");
                    out.println(Sql.attendanceRecord(transferFiles.getId()));
                }
            }
//        } catch (IOException | ClassNotFoundException | SQLException e) {
//            e.printStackTrace();
        }catch (Exception e){
         e.printStackTrace();
        }
    }
}
